<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>

      <!-- 登录表单区域 -->
      <el-form class="login_form"  :rules="loginRules" :model="loginForm" ref="loginFormRef">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" :prefix-icon="User" style="height: 35px;"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" :prefix-icon="Lock" style="height: 35px;"></el-input>

        </el-form-item>

        <!-- 验证码 -->
        <el-form-item prop="code">
          <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63% ;height: 35px;">

          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>

        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="handleLogin">登录</el-button>
          
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref, onMounted } from 'vue'
import { getCodeImg } from "@/apis/login"
import { useUserStore } from "@/stores/user"
import { useRouter,useRoute } from "vue-router"
import { ElMessage } from 'element-plus' // 引入 ElMessage
const userStore = useUserStore()
const router = useRouter()
const route = useRoute() // 获取当前路由信息
// 验证规则
const loginRules = {
  username: [
    { required: true, trigger: "blur", message: "请输入您的账号" }
  ],
  password: [
    { required: true, trigger: "blur", message: "请输入您的密码" }
  ],
  code: [
    { required: true, trigger: "change", message: "请输入验证码" }
  ]
}
const redirect = ref('/') // 默认重定向路径
const codeUrl = ref('')
const loginForm = reactive({
  username: "admin",
  password: "admin123",
  rememberMe: false,
  code: "",
  uuid: ""
})

// 引用表单组件
const loginFormRef = ref(null)

const getCode = () => {
  getCodeImg().then(res => {
    codeUrl.value = "data:image/gif;base64," + res.data.img
    loginForm.uuid = res.data.uuid
  })
}

// 登录处理
const handleLogin = () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      console.log("验证成功，开始登录")
      userStore.login(loginForm).then(res => {
        if (res.code === 200) {
          ElMessage.success(res.msg || '登录成功') // 登录成功提示
          console.log(res)
          window.localStorage.setItem('token', res.token) // 保存token到sessionStorage
          console.log(res.token)
          router.push({ path: redirect.value }).catch(() => {})
        } else {
          ElMessage.error(res.msg || '登录失败') // 失败提示
          getCode() // 刷新验证码
        }
      }).catch((error) => {
        console.error(error)
        ElMessage.error('登录出现错误，请稍后重试') // 请求错误提示
      })
    } else {
      console.log("验证失败")
    }
  })
}

onMounted(() => {
  getCode()
  redirect.value = route.query.redirect || '/' // 设置重定向路径
})
</script>

<style lang="scss" scoped>
.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  margin-left: 20px;
  height: 38px;
}

.login_container {
  background-color: #3D315B;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #F2C641;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
}
</style>